package com.hdss996.dictationread.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;


public class SysToolUtil {

    /**
     * 普通消息Alert
     *
     * @param title
     * @param text
     * @param btn
     * @param context
     */
    public static void msgAlert(final String title, final String text, final String btn, final Context context) {
        new Thread() {
            public void run() {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(title).setMessage(text).setPositiveButton(btn, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                        AlertDialog alertDialog = builder.create();
                        alertDialog.setCanceledOnTouchOutside(false);
                        alertDialog.show();
                    }
                });
            }
        }.start();
    }

    /**
     * 下载/打开网址Alert
     *
     * @param title
     * @param text
     * @param btn
     * @param context
     */
    public static void dwnAlert(final String title, final String text, final String btn, final String durl, final Context context) {
        new Thread() {
            public void run() {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(title).setMessage(text)
                                .setNegativeButton(btn, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(durl)));
                                    }
                                })
                                .setNeutralButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                        AlertDialog alertDialog = builder.create();
                        alertDialog.setCanceledOnTouchOutside(false);
                        alertDialog.show();
                    }
                });
            }
        }.start();
    }


    // 定义一个方法用于显示带有自定义布局的警报对话框
    public static void showCustomAlertDialog(final String title, final String btn, final Context context, int customLayoutId) {
        // 在新的线程中执行对话框的显示操作
        new Thread() {
            public void run() {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        // 创建一个 AlertDialog.Builder 对象
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);
                        builder.setTitle(title);

                        // 加载自定义布局
                        View customView = LayoutInflater.from(context).inflate(customLayoutId, null);
                        builder.setView(customView);

                        // 设置按钮
                        builder.setPositiveButton(btn, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });

                        // 创建并显示对话框
                        AlertDialog alertDialog = builder.create();
                        alertDialog.setCanceledOnTouchOutside(false); // 设置点击外部区域不消失
                        alertDialog.show();
                    }
                });
            }
        }.start();
    }


    /**
     * 判断应用是否存在
     *
     * @param packageName
     * @return
     */
//    public boolean isAppExist(String pkgName, Context context) {
//        ApplicationInfo info;
//        try {
//            info = context.getPackageManager().getApplicationInfo(pkgName, 0);
//        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
//            info = null;
//        }
//        return info != null;
//    }
    public boolean isAppExist(String packageName, Context context) {
        try {
            context.getPackageManager().getApplicationInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    /**
     * 判断应用是否为系统应用
     *
     * @param packageName
     * @return
     */
    public boolean isSystemApp(String packageName, Context context) {
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(packageName, 0);
            if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0){
                return true;
            }else {
                return false;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }


    public void uninstallAPP(String packageName, Context context) {
        Uri packageURI = Uri.parse("package:".concat(packageName));
        Intent intent = new Intent(Intent.ACTION_DELETE);
        intent.setData(packageURI);
        context.startActivity(intent);
    }

    /**
     * 设定默认配置
     */
    public static void setDefConfig(Context context) {
        //记录初次启动
        SharedPreferences app_run = context.getSharedPreferences("app_run", Context.MODE_PRIVATE);
        SharedPreferences.Editor app_run_editor = app_run.edit();
        app_run_editor.putBoolean("runtime", true);
        app_run_editor.apply();
        //配置默认引擎语言
        SharedPreferences tts_lan = context.getSharedPreferences("tts_lan", Context.MODE_PRIVATE);
        SharedPreferences.Editor tts_lan_editor = tts_lan.edit();
        tts_lan_editor.putString("tts_lan", "CHINA");
        tts_lan_editor.apply();
        //配置排号模式默认数据
        SharedPreferences config_dic_mode = context.getSharedPreferences("config_dic_mode", Context.MODE_PRIVATE);
        SharedPreferences.Editor config_dic_mode_editor = config_dic_mode.edit();
        config_dic_mode_editor.putString("start_str", "听写即将开始，请做好准备！");
        config_dic_mode_editor.putString("end_str", "听写已结束，请检查！");
        config_dic_mode_editor.apply();

    }

}
